//
//  HeaderViewSty1.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/9/26.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "HeaderViewSty1.h"
#import "Masonry.h"
#import "UIColor+CL.h"
#import "MyButtonMainPage.h"
#import "EducationAdminClient_URL.h"
#import "GetData.h"

@implementation HeaderViewSty1

-(instancetype)initWithDelegate:(id)delegate andBlock:(void (^)(MenuModel *))block{
    self = [super init];
    if (self) {
        
        _block = block;
        _delegate = delegate;
        
        [self getDocumentData];
    }
    
    return self;
}


#pragma mark - 取出数据源
-(void)getDocumentData{
    
    //[[[GetData alloc]init]getDocumentTopViewData:^(NSMutableArray *menuArr, NSMutableArray *secondArr, NSMutableArray *dataArr) {
        //_dataArr =dataArr;
    //}];
    NSString *filename=getSaveFile(userName(),LOGINFOUNCTION);   //获取路径
    NSString *path=[filename stringByAppendingPathComponent:LOGINOK];
    
    NSData *dataNow = [NSData dataWithContentsOfFile:path];
    NSDictionary * dic;
    if(dataNow){
        
        dic = [NSJSONSerialization JSONObjectWithData:dataNow options:NSJSONReadingMutableContainers error:nil];  //读取数据
    }
    //    NSLog(@"%@",dic);
    
    NSArray *arr = [dic valueForKey:@"homePageList"];
    NSMutableArray *mutArr = [[NSMutableArray alloc]init];
    
    for (NSDictionary *dica in arr) {
        MenuModel *model = [[MenuModel alloc]init];
        model = [model initWithDictionary:dica error:nil];
        [mutArr addObject:model];
    }
    _dataArr = mutArr;
    
    [self createCollectionViewHeaderViewWithData:_dataArr];
    
}

-(UIView *)createCollectionViewHeaderViewWithData:(NSArray *)dataArr{
    
    UIView *StuSuperView = self;
    
    UIView *baseView = [[UIView alloc]init];
    baseView.backgroundColor = [UIColor whiteColor];
    
    [StuSuperView addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(200/2));
        make.left.equalTo(StuSuperView.mas_left);
        make.right.equalTo(StuSuperView.mas_right);
        make.top.equalTo(StuSuperView.mas_top);
    }];
    
    for (int i = 0; i < dataArr.count; i ++) {
        
        MenuModel *model = dataArr[i];
        
        MyButtonMainPage *myBtn = [[MyButtonMainPage alloc]init];
        myBtn.Btntag = i;
        [baseView addSubview:myBtn];

        [myBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(baseView.mas_left).offset(0+[UIScreen mainScreen].bounds.size.width/4 * i);
            make.width.equalTo(@(([UIScreen mainScreen].bounds.size.width/4)));
            make.height.equalTo(@((190)/2));
            make.top.equalTo(StuSuperView.mas_top);
            //             make.bottom.equalTo(baseView.mas_bottom).offset(-20);
            
        }];
        
        NSMutableString *strMenuName = [NSMutableString stringWithFormat:@"%@", model.menuName];
        if ([strMenuName rangeOfString:@"手机端"].location != NSNotFound) {
            [strMenuName replaceCharactersInRange:[strMenuName rangeOfString:@"手机端"] withString:@""];
            model.menuName = strMenuName;
        }

        [myBtn setTitle:model.menuName forState:UIControlStateNormal];
        myBtn.titleLabel.font = [UIFont systemFontOfSize:28/2];
        [myBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        NSString *strI = [NSString stringWithFormat:@"edus_see_%d", i+1%4];
        if(i==0){//汉普森----主项目可取消
            strI=@"edus_see_today";
        }else if (i==1){
            strI=@"edus_see_tomorrow";
        }else if (i==2){
            strI=@"edus_see_vip";
        }else if (i==3){
            strI=@"edus_see_group";
        }
        [myBtn setImage:[UIImage imageNamed:strI] forState:UIControlStateNormal];
        [myBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];

    }
    if(dataArr.count<0){ //dataArr.count<=0  显示4个圆按钮
                MyButtonMainPage *myBtn = [[MyButtonMainPage alloc]init];
            myBtn.Btntag = 0;
            [baseView addSubview:myBtn];
            
            MyButtonMainPage *myBtn1 = [[MyButtonMainPage alloc]init];
            myBtn1.Btntag = 1;
            [baseView addSubview:myBtn1];
            
            MyButtonMainPage *myBtn2 = [[MyButtonMainPage alloc]init];
            myBtn2.Btntag = 2;
            [baseView addSubview:myBtn2];
            
            MyButtonMainPage *myBtn3 = [[MyButtonMainPage alloc]init];
            myBtn3.Btntag = 3;
            [baseView addSubview:myBtn3];
            
            [myBtn setTitle:@"扫码考勤" forState:UIControlStateNormal];
            [myBtn1 setTitle:@"报表管理" forState:UIControlStateNormal];
            [myBtn2 setTitle:@"消息提醒" forState:UIControlStateNormal];
            [myBtn3 setTitle:@"系统设置" forState:UIControlStateNormal];

        
            
            myBtn.titleLabel.font = [UIFont systemFontOfSize:28/2];
            myBtn1.titleLabel.font = [UIFont systemFontOfSize:28/2];
            myBtn2.titleLabel.font = [UIFont systemFontOfSize:28/2];
            myBtn3.titleLabel.font = [UIFont systemFontOfSize:28/2];
            
            [myBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [myBtn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [myBtn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [myBtn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            
            
            [myBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(baseView.mas_left).offset(0);
                make.width.equalTo(myBtn1.mas_width);
                make.height.equalTo(@((190)/2));
                make.top.equalTo(StuSuperView.mas_top);
                //             make.bottom.equalTo(baseView.mas_bottom).offset(-20);
                
            }];
            [myBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(myBtn.mas_right);
                make.width.equalTo(myBtn.mas_width);
                make.height.equalTo(myBtn.mas_height);
                make.bottom.equalTo(myBtn.mas_bottom);
            }];
            
            [myBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(myBtn1.mas_right);
                make.width.equalTo(myBtn1.mas_width);
                make.height.equalTo(myBtn1.mas_height);
                make.bottom.equalTo(myBtn1.mas_bottom);
                //            make.right.equalTo(myBtn3.mas_right).offset(-20);
            }];
            
            [myBtn3 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(myBtn2.mas_right);
                make.width.equalTo(myBtn2.mas_width);
                make.height.equalTo(myBtn2.mas_height);
                make.bottom.equalTo(myBtn2.mas_bottom);
                make.right.equalTo(baseView.mas_right).offset(-0);
            }];
            
            
            [myBtn setImage:[UIImage imageNamed:@"edus_see_1"] forState:UIControlStateNormal];
            [myBtn1 setImage:[UIImage imageNamed:@"edus_see_2"] forState:UIControlStateNormal];
            [myBtn2 setImage:[UIImage imageNamed:@"edus_see_3"] forState:UIControlStateNormal];
            [myBtn3 setImage:[UIImage imageNamed:@"edus_see_4"] forState:UIControlStateNormal];
            
            
            [myBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
            [myBtn1 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
            [myBtn2 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
            [myBtn3 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
             
                          }
    UIButton *nameCellBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [StuSuperView addSubview:nameCellBtn];
    [nameCellBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(StuSuperView.mas_bottom);
        make.height.equalTo(@(160/2));
        make.left.equalTo(StuSuperView.mas_left);
        make.right.equalTo(StuSuperView.mas_right);
    }];
    nameCellBtn.backgroundColor = [UIColor whiteColor];
    
    
    
    // 目前没用上 ， 名称 点击事件
    if(self.delegate){
        [nameCellBtn addTarget:self.delegate action:@selector(nameCellBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIImageView *im = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"task"]];
    [nameCellBtn addSubview:im];
    [im mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameCellBtn.mas_left).offset(30/2);
        make.centerY.equalTo(nameCellBtn.mas_centerY);
    }];
    UILabel *nameLabel = [[UILabel alloc] init];
    [nameCellBtn addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameCellBtn.mas_top).offset(42/2);
        make.height.equalTo(@(30/2));
        make.left.equalTo(im.mas_right).offset(26/2);
    }];
    [nameLabel sizeToFit];
    
    
    //nameLabel.text = @"待办任务";
    //这里添加功能判断
    if (getEnglishLanguage()) {
        nameLabel.text = @"To-do Task";
    }else{
        nameLabel.text = @"待办事务";
    }
    

    nameLabel.textColor = [UIColor colorWithHex:0x333333];
    nameLabel.font = [UIFont systemFontOfSize:32/2];
    
    
    UILabel *KindStuLabel = [[UILabel alloc] init];
    [nameCellBtn addSubview:KindStuLabel];
    [KindStuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(16/2);
        make.left.equalTo(nameLabel.mas_left);
        make.height.equalTo(@(30/2));
//        KindStuLabel.backgroundColor = [UIColor redColor];
    }];
    [KindStuLabel sizeToFit];
    NSDate* date = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MM/dd"];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:date];
    NSInteger weekDay = [comp weekday];
    NSLog(@"选中周是周几%zd",weekDay);
    NSString *str = [df stringFromDate:date];
    NSString *weekStr;
    
#pragma mark - ********星期几的修改地点*******
    
    //这里添加功能判断
    if (getEnglishLanguage()) {
        if (weekDay == 1) {
            weekStr = @"Sunday";
        }else{
            switch (weekDay) {
                case 2:
                    weekStr = @"Monday";
                    break;
                case 3:
                    weekStr = @"Tuesday";
                    break;
                case 4:
                    weekStr = @"Wednesday";
                    break;
                case 5:
                    weekStr = @"Thursday";
                    break;
                case 6:
                    weekStr = @"Friday";
                    break;
                case 7:
                    weekStr = @"Saturday";
                    break;
                default:
                    break;
            }
        }

    }else{
        if (weekDay == 1) {
            weekStr = @"周日";
        }else{
            switch (weekDay) {
                case 2:
                    weekStr = @"周一";
                    break;
                case 3:
                    weekStr = @"周二";
                    break;
                case 4:
                    weekStr = @"周三";
                    break;
                case 5:
                    weekStr = @"周四";
                    break;
                case 6:
                    weekStr = @"周五";
                    break;
                case 7:
                    weekStr = @"周六";
                    break;
                default:
                    break;
            }
        }

    }

        KindStuLabel.text = [NSString stringWithFormat:@"%@ %@", str,weekStr];
    KindStuLabel.font = [UIFont systemFontOfSize:28/2];
    KindStuLabel.textColor = [UIColor colorWithHex:0xa1abb3];
    UIView *bgView = [[UIView alloc] init];
    [StuSuperView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(nameCellBtn.mas_top);
        make.height.equalTo(@(10/2));
        make.left.equalTo(StuSuperView.mas_left);
        make.right.equalTo(StuSuperView.mas_right);
        
    }];
    bgView.backgroundColor = [UIColor colorWithHex:0xf7f7f7];
    
    
    
    
    return StuSuperView;
}

-(void)btnClick:(MyButtonMainPage *)btn{
    NSLog(@"%ld",(long)btn.Btntag);
    if (_dataArr.count<btn.Btntag+1) {
        return;
    }
    NSDictionary *dic = _dataArr[btn.Btntag];
    _block(dic);
    /**if (_delegate && [_delegate respondsToSelector:@selector(btnClick:andData:)]) {
        [_delegate btnClick:btn andData:_dataArr[btn.Btntag]];
    }*/
    
}

@end
